<?php

/*//. Class edgeIcon
  ../ Extension of GtkStatusIcon --
      public function __construct(void);
      public function is_ready(void);
      
      public function on_left_click(void);
      //. present the window to the user when direct click on the tray icon.      
      
      public function on_right_click(void);
      //. present the tray icon menu system on right click.
      
      public function on_menu_update(void);
      //. callback for updating the widgets on the menu.
      
      public function on_automatic_display(void);
      public function on_automatic_hide(void);
      //. automatically display or hide the menu system (to use as a
      //. notificatin popup of sorts)
.//*/

class edgePopup extends GtkWindow {

	public function __construct() {
		parent::__construct();

		$menu = new GtkMenuItem;
		$style = $menu->get_style();
		$this->set_style($style);
		$this->ensure_style();

		$this->realize();		
		
		$this->set_gravity(Gdk::GRAVITY_SOUTH_EAST);
		$this->set_accept_focus(false);
		$this->set_focus_on_map(false);
		$this->set_decorated(false);	
		$this->set_keep_above(true);
		$this->set_skip_taskbar_hint(true);
		$this->set_border_width(0);
		$this->set_opacity(0.85);
		$this->set_property('allow_shrink',true);
		$this->set_title('edgecast-notify');

		$this->hbox = new GtkHBox;
		$this->image = GtkImage::new_from_file(EXECROOT.'/share/edgecast/popup-icon.png');
		$this->label = new GtkLabel;
		$this->hbox->pack_start($this->image,false,false,0);
		$this->hbox->pack_start($this->label,true,true,0);
		$this->hbox->show_all();
		edge::update_ui();
		
		$this->add($this->hbox);
		return;
	}
	
	public function place() {

		$this->unrealize();
		$this->realize();

		edge::update_ui();
		
		list($w,$h) = $this->get_size();
		
//		echo("$w $h\n");	
	
		$this->move((edge::$deskw-$w),(edge::$deskh-$h));
		
		edge::update_ui();
		
		$this->show_all();

		return;
	}

}

class edgeIcon extends GtkStatusIcon {

	private $menu = null;
	private $menubtn,$menutime;
	private $iter = 0;
	public $alive;
	
	public function __construct() {
		parent::__construct();

		$this->set_from_file(EXECROOT.'/share/edgecast/icon.png');
		edge::update_ui();
		
		$this->connect_simple('activate',array($this,'on_left_click'));
		$this->connect_simple('popup-menu',array($this,'on_right_click'));

		$this->popup = new edgePopup;

		$this->is_ready();
		return;
	}

	public function is_ready() {
		$this->alive = true;
		
		if($this->iter < 5 && !$this->is_embedded()) {
			Gtk::timeout_add(750,array($this,'is_ready'));
			++$this->iter;
			$this->alive = false;
		}
		
		else if(!$this->is_embedded()) {
			echo("Warning: Unable to create Tray Icon. Please insure that your system's tray is enabled.\n");
			$this->alive = false;
		}	
		
		return;
	}

	public function on_left_click() {

		if(edge::$w->window->get_state() & Gdk::WINDOW_STATE_ICONIFIED) {
			edge::$w->present();
		} else {
			edge::$w->iconify();
		}
	
		return;
	}	

	public function on_right_click() {
		$this->on_automatic_display();
		return;
	}
	
	public function on_automatic_display() {

		if($this->popup->is_visible()) { return; }	
	
		$this->popup_update(true);
		$this->popup->place();
		Gtk::timeout_add(1000,array($this,'popup_update'));
		Gtk::timeout_add(5000,array($this,'on_automatic_hide'));
		return false;
	}
	
	public function on_automatic_hide() {
		$this->popup->hide();
		return false;
	}

	public function popup_update($force = false) {
		
		if($force || $this->popup->is_visible()) {
			if(edge::$state == edge::STATE_PLAYING) {
				$this->popup->label->set_markup(sprintf(
					"<span size='larger' weight='bold'>%s</span> \n".
					"<b>%s:</b> %s. ",
					edge::$title,
					edge::$stream,
					edge::$currenttitle					
				));
			}

			else if(edge::$state == edge::STATE_STOPPED) {
				$this->popup->label->set_markup(sprintf(
					"<span size='larger' weight='bold'>%s</span> \nCurrently Stopped. ",
					edge::$title				
				));
			}
			
			else if(edge::$state == edge::STATE_BUFFERING) {
				$this->popup->label->set_markup(sprintf(
					"<span size='larger' weight='bold'>%s</span> \nBuffering %s. ",
					edge::$title,
					edge::$stream	
				));
			}

			else if(edge::$state == edge::STATE_CONNECTING) {
				$this->popup->label->set_markup(sprintf(
					"<span size='larger' weight='bold'>%s</span> \nConnecting to %s. ",
					edge::$title,
					edge::$stream	
				));
			}
			
			return true;
		} else {
			return false;
		}

	}
}

?>